//
//  HMCircleAnimator.m
//  shapeAnimation
//
//  Created by acer on 16/10/24.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "HMCircleAnimator.h"
@interface HMCircleAnimator()<UIViewControllerAnimatedTransitioning,CAAnimationDelegate>

@end

@implementation HMCircleAnimator
{
    /// 是否展现标记
    BOOL _isPresented;
    
    id <UIViewControllerContextTransitioning> _transitionContext;
}

- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
    _isPresented = YES;
    return self;
}

- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
    _isPresented = NO;
    return self;
}

//- (nullable id <UIViewControllerInteractiveTransitioning>)interactionControllerForPresentation:(id <UIViewControllerAnimatedTransitioning>)animator;

//- (nullable id <UIViewControllerInteractiveTransitioning>)interactionControllerForDismissal:(id <UIViewControllerAnimatedTransitioning>)animator;

//- (nullable UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(nullable UIViewController *)presenting sourceViewController:(UIViewController *)source NS_AVAILABLE_IOS(8_0);
#pragma mark - UIViewControllerAnimatedTransitioning
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext
{
    return 0.25;
}
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{
    UIView *containerView = [transitionContext containerView];
    
    UIView *formView = [transitionContext viewForKey:UITransitionContextFromViewKey];
    UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
    UIView *view;
    if (_isPresented) {
        view = toView;
    } else {
        view = formView;
    }
    if (_isPresented) {
        
        [containerView addSubview:view];
    }
    
    [self circleAnimWithView:view];
    

    _transitionContext = transitionContext;
}

-(void)circleAnimWithView:(UIView *)view
{
    CAShapeLayer *shapLayer = [CAShapeLayer layer];
    //画内切圆
    float screenWidth = view.bounds.size.width;
    float screenHeight = view.bounds.size.height;
    CGRect rect = CGRectMake(screenWidth - 51, 23, 40, 40);
    UIBezierPath *spath = [UIBezierPath bezierPathWithOvalInRect:rect];
    shapLayer.path = spath.CGPath;
    //    画外切圆
    float radius = sqrtf(screenWidth * screenWidth + screenHeight * screenHeight);
//    CGRect endRect = CGRectMake(screenWidth - 50, 50, -radius, radius);
    CGRect endRect = CGRectInset(rect, -radius, -radius);
    UIBezierPath *endPath = [UIBezierPath bezierPathWithOvalInRect:endRect];
    //    罩层
    view.layer.mask = shapLayer;
    //    动画
    CABasicAnimation *anima = [CABasicAnimation animationWithKeyPath:@"path"];
//    anima.duration = 0.4;
    if (_isPresented) {
        
        anima.fromValue = (__bridge id _Nullable)(spath.CGPath);
        anima.toValue = (__bridge id _Nullable)(endPath.CGPath);
    } else {
        anima.toValue = (__bridge id _Nullable)(spath.CGPath);
        anima.fromValue = (__bridge id _Nullable)(endPath.CGPath);
    }
    anima.delegate = self;
    anima.fillMode = kCAFillModeForwards;
    anima.removedOnCompletion = NO;
    [shapLayer addAnimation:anima forKey:nil];
}


- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
        [_transitionContext completeTransition:YES];
}
@end
